package com.skt.common_hr.work.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.work.WorkFacade;
	import com.skt.common_hr.work.model.ReqWorkTimeProxy;
	import com.skt.common_hr.work.model.VacationCodeProxy;
	import com.skt.common_hr.work.model.vo.ReqVacationVO;
	import com.skt.common_hr.work.model.vo.VacationCodeVO;
	import com.skt.common_hr.work.view.components.ReqWorkTimeView;
	
	import flash.events.Event;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	public class ReqWorkTimeMediator2 extends Mediator
	{
		public static const NAME:String = "ReqWorkTimeMediator"; 

		public var baseDate:Date;	
		public var vo:ReqVacationVO;
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function ReqWorkTimeMediator2( viewComponent:Object )
        {
            super( NAME, viewComponent );
            view.callLater(initView);
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		private function initView():void
 		{
 			// 각 버튼들의 Event Handler를 설정하고
 			view.addEventListener(ReqWorkTimeView.CONFIRM, onConfirm);
 			view.addEventListener(ReqWorkTimeView.CANCEL, onCancel);
			
			// 휴가 코드리스트를 가져온다.
 			var codeProxy:VacationCodeProxy = facade.retrieveProxy(VacationCodeProxy.NAME) as VacationCodeProxy;
			view.codeAll = codeProxy.codeList;
			view.codeVacation = codeProxy.vacationList;
			view.codeWorkTime = codeProxy.worktimeList;
			initData();
 		}
		
		private function initData():void 
		{
			// 입력레코드를 생성한다.
 			vo = new ReqVacationVO;
 			vo.SEQNO = "0";
 			vo.PERNR = vo.IPERNR = Variables.myPernr;
 			vo.BEGDA = new Date(baseDate);
 			vo.ENDDA = new Date(baseDate);
 			vo.BEGUZ = new Date(baseDate);
 			vo.ENDUZ = new Date(baseDate);
 			vo.APM = "A";
 			
 			view.startDate = new Date(baseDate);
		}
		

 		private function clearView():void
 		{
 			trace("clear Work");
 		}
 		
 		private function onConfirm(event:Event):void
 		{
 			if(view.tbType.selectedIndex==0) {
 				submitWorkTime();
 			}
 			else {
 				submitVacation();
 			}
 			PopUpManager.removePopUp(view);
 		}
 		
 		private function submitWorkTime():void
 		{
 			var table:Array = new Array();
 			var rec:ReqVacationVO = new ReqVacationVO();
 			rec.SEQNO = "1";
 			rec.AWART = VacationCodeVO(view.cbWorkType.selectedItem).code;
 			rec.PERNR = rec.IPERNR = Variables.myPernr;
 			rec.DGBN  = "2";
 			rec.BEGDA = new Date(view.startDate);
 			rec.ENDDA = new Date(rec.BEGDA);
 			trace(view.startTime);
 			trace(view.endTime);
 			rec.BEGUZ = new Date(view.startTime);
 			rec.ENDUZ = new Date(view.endTime);
 			if(Number(rec.BEGUZ) > Number(rec.ENDUZ) ) {
 				//PsHrAlert.showMessage("근태 종료시간이 시작시간 보다 작습니다.", "입력오류", view);
 				return ;
 			}
 			table.push(rec);
			var proxy:ReqWorkTimeProxy = facade.retrieveProxy(ReqWorkTimeProxy.NAME) as ReqWorkTimeProxy;
			proxy.InsertWorkTime("2", table, "");
 		}

		private function submitVacation():void
		{
			
		}

 		private function onCancel(event:Event):void
 		{
 			PopUpManager.removePopUp(view);
 		}
 		
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					WorkFacade.RESTART_POPUP,
					//WorkFacade.INSERT_REQ_WORKTIME_COMPLETE,
					//WorkFacade.INSERT_REQ_WORKTIME_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case WorkFacade.RESTART_POPUP:
					initData();
					break;
				/*
				case WorkFacade.INSERT_REQ_WORKTIME_COMPLETE:
					Hourglass.remove();
					PopUpManager.removePopUp(view);
					break;
				case WorkFacade.INSERT_REQ_WORKTIME_FAILED:
					Hourglass.remove();
					PsHrAlert.showMessage("("+note.getBody().errCode+")"+note.getBody().errText, "RFC 오류", view);
					break;*/
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():ReqWorkTimeView
        {
            return viewComponent as ReqWorkTimeView;
        }
	}
}